home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3933 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: mail2news.demon.co.uk!hpl3sn03.cern.ch
  2. From: Dan Pop <danpop@mail.cern.ch>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Use of 'htonl' on DEC Alpha 2100 VME server
  5. Date: Thu, 1 Feb 1996 02:19:13 +0100
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <9602010119.AA21648@dxmint.cern.ch>
  8. References: <4eok6v$15do@morse.ukonline.co.uk>
  9. X-NNTP-Posting-Host: hpl3sn03.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11. X-Mail2News-Path: dxmint.cern.ch!hpl3sn03.cern.ch
  12.  
  13. david.tuson@ukonline.co.uk (Dave T.) writes:
  14.  
  15. >I am currently developing an application in which several C/Ada models
  16. >run on a DEC Alpha 2100 VME server which is running DEC Unix.  Data
  17. >produced by these models is used to drive applications running on an
  18. >SGI Indigo2  by passing the data over an ethernet connection.
  19. >
  20. >Prior to passing the data across the network I use the ntohl()
  21.                                                         ^^^^^^^
  22. I guess you meant htonl() but this doesn't make any difference.  Both
  23. functions should behave identically on Digital UNIX (i.e. revert the byte
  24. order of a 32-bit integer).
  25.  
  26. >function to reverse the order of the bytes in each 32 bit word.  When
  27. >I do this I find that the four bytes in memory immediately after those
  28. >swapped by ntohl are then all set to zero.  Can anyone tell me why
  29. >this happens and how it can be avoided? Thanks.
  30.  
  31. This is principially impossible: htonl() expects as its argument an
  32. unsigned int and produces as result another unsigned int.  It cannot
  33. corrupt any byte because it is not passed a pointer.
  34.  
  35. My guess is that htonl() is a red herring and you're corrupting the
  36. data yourself, like this:
  37.  
  38.     unsigned long *ptr = buff;
  39.     *ptr = htonl(some_value);
  40.  
  41. On Digital UNIX this will set to zero the next four bytes, indeed, because
  42. a long is 8 bytes on that platform and the byte order is little-endian.
  43. So, the assignment will convert the result returned by htonl() to a 64-bit
  44. long and store it, which will result in the first 4 bytes set to the value
  45. returned by htonl() and the next 4 bytes set to zero.
  46.  
  47. Never assume that a certain type has the same size on every platform.
  48. On Digital UNIX, int's and long's have different sizes!
  49.  
  50. Dan
  51. -- 
  52. Dan Pop
  53. CERN, CN Division
  54. Email: danpop@mail.cern.ch 
  55. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  56.